﻿using System;
using System.Threading;
using System.Xml.Linq;
using System.Reflection;
using EDTS.Model.Weixin;
using EDTS.Logic.Weixin;
using EDTS.Weixin.Requests;
using EDTS.Weixin;
using EDTS.Logic.Weixin.Processors;
namespace EDTS.WinServices
{
    /// <summary>
    /// 微信接收到的消息，轮巡处理
    /// </summary>
    public class WeixinReceiver:Task
    {
        public WeixinReceiver(ISchedule schedule) : base(schedule) { }

        public override void Execute(object param)
        {
            base.Execute(param);
            try
            {
                Assembly asm = Assembly.GetAssembly(typeof(IProcessor));
                IProcessor pro = null;
                WxProcessorModel wxPro = null;
                object msg;
                
                while (Common.Power)
                {
                    var lst = WxRequestLogic.Gets(new WxRequestQuery() { Status = (int)WxRequestStatus.Waiting });
                    if (lst.TotalRecordsCount > 0)
                    {
                        foreach (var req in lst)
                        {
                            try
                            {
                                switch (req.MsgType)
                                {
                                    case "Event":
                                        var eventMsg = (EventMsg)MessageFactory.GetRequest(XDocument.Parse(req.Description));
                                        msg = eventMsg;
                                        wxPro = WxProcessorLogic.GetModel("Event", eventMsg.Event.ToString().ToLower());
                                        break;
                                    default:
                                        WxRequestLogic.ChangeStatus(req.ID, WxRequestStatus.Pass);
                                        continue;
                                }

                                if (wxPro!=null && wxPro.Status == (int)Model.Status.Active)
                                {
                                    pro = (IProcessor)asm.CreateInstance(wxPro.FullName, true, BindingFlags.Default, null, new object[] { msg }, null, null);
                                    pro.Excute();
                                    WxRequestLogic.ChangeStatus(req.ID, WxRequestStatus.Complete);
                                }
                                else
                                    WxRequestLogic.ChangeStatus(req.ID, WxRequestStatus.Pass);
                            }
                            catch (Exception ex)
                            {
                                WxRequestLogic.ChangeStatus(req.ID, WxRequestStatus.Failed, ex.Message);
                            }
                        }
                    }
                    Thread.Sleep(1000);
                }
            }
            catch (Exception ex) {
                Utils.LogHelper.SaveErrorLog(ex, Common.ErrPath);
            }
        }
    }
}
